package com.duobaoyu.dby.supply.biz.util;

import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.supply.biz.exception.SupplyChainBizException;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * hib校验
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/25 16:25
 */
public class ValidationUtils {


	public static Validator instance() {
		return Validation.byProvider(HibernateValidator.class).configure()
				.failFast(true).buildValidatorFactory().getValidator();
	}

	public static <T> void check(T t) {
		Set<ConstraintViolation<T>> validate = ValidationUtils.instance().validate(t);
		if (CollectionUtils.isNotEmpty(validate)) {
			ConstraintViolation<T> next = validate.iterator().next();
			SupplyChainBizException.fail(next.getPropertyPath() + StringPool.COLON + next.getMessage());
		}
	}
}
